package org.example.service.client;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.example.HelloServiceGrpc;
import org.example.helloProto;

import java.util.Iterator;

/**
 * @Author : 叶荻风
 * @create : 2024/6/20 19:38
 * server stream
 */
public class GrpcClientServerSream {
    public static void main(String[] args) {
        //1.创建通信的管道
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost",9000).usePlaintext().build();
        //2.获取代理对象stub
        HelloServiceGrpc.HelloServiceBlockingStub stub = HelloServiceGrpc.newBlockingStub(managedChannel);
        helloProto.HelloRequest.Builder builder = helloProto.HelloRequest.newBuilder();
        builder.setUsername("difeng");
        helloProto.HelloRequest request = builder.build();
        Iterator<helloProto.HelloResponse> helloResponseIterator = stub.c2ss(request); //阻塞式 开发
        while(helloResponseIterator.hasNext()){
            helloProto.HelloResponse next = helloResponseIterator.next();
            System.out.println("result:" + next.getUsername());
        }

        managedChannel.shutdown();
    }
}
